package com.zhisolution.zhicms.util;

import java.awt.Color;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Random;
import java.util.Vector;

public class StringCode {

	private static int[] key = { 181, 141, 91, 193, 61, 59, 39, 75, 135, 155,
			189, 57, 9, 81, 171, 231, 193, 123, 132 };

	/**
	 * cleanString Ensure that the string has no characters that are out of
	 * bounds of the normal character set. Used to avoid char conversion errors
	 * when saving strings to the datavas.base.
	 */
	public static String cleanString(String value) {
		// Right now use the simple screen of 1 to 127
		// Check to see if value is ok - if yet return it
		if (value == null) {
			return value;
		}

		int l = value.length();
		int i;
		boolean ok = true;

		for (i = 0; i < l; i++) {
			if (value.charAt(i) < 1 || value.charAt(i) > 127) {
				ok = false;
				break;
			}
		}
		if (ok) {
			return value;
		}

		// Not OK, build new string replacing unwanted character values
		StringBuffer result = new StringBuffer(value);
		for (i = 0; i < l; i++) {
			if (result.charAt(i) < 1 || result.charAt(i) > 127) {
				result.setCharAt(i, '-');
			}
		}
		return result.toString();
	}

	public static String encodeStringToHex(String string) {
		byte[] value = string.getBytes();
		xorByteArray(value);
		String result = toHexString(value);
		return result;
	}

	public static String decodeHexString(String value) {
		byte[] bytes = parseHexString(value);
		xorByteArray(bytes);
		String result = new String(bytes);
		return result;
	}

	public static byte[] encodeString(String string) {
		byte[] value = string.getBytes();
		xorByteArray(value);
		return value;
	}

	public static String decodeString(byte[] value) {
		xorByteArray(value);
		return new String(value);
	}

	public static byte[] parseHexString(String value) {
		// parse every two bytes
		int size = value.length() / 2;
		byte[] result = new byte[size];

		for (int i = 0; i < size; i++) {
			byte b1 = parseHexChar(value.charAt(i * 2));
			byte b2 = parseHexChar(value.charAt(i * 2 + 1));
			result[i] = (byte) ((b1 << 4) + b2);
		}
		return result;
	}

	public static String toHexString(byte[] buffer) {
		StringBuffer result = new StringBuffer();

		for (int i = 0; i < buffer.length; i++) {
			String val = Integer.toHexString((int) buffer[i] & 0xff);
			if (val.length() == 1) {
				result.append("0");
			}
			result.append(val);
		}
		return result.toString();
	}

	private static void xorByteArray(byte[] value) {
		int c = 0;
		for (int i = 0; i < value.length; i++) {
			value[i] = (byte) (value[i] ^ key[c]);
			c = (c + 1) % key.length;
		}
	}

	private static byte parseHexChar(char c) {
		if (c >= '0' && c <= '9') {
			return (byte) (c - '0');
		}
		if (c >= 'a' && c <= 'z') {
			return (byte) ((c - 'a') + 10);
		}
		if (c >= 'A' && c <= 'Z') {
			return (byte) ((c - 'A') + 10);
		}
		return 0;
	}
	/**
	 * 加密
	 * @param source
	 * @return
	 */
	public static String encrypt(String source) {
		if (source == null) {
			return "";
		}
		return (toHexString(encodeString(source)));
	}
	/**
	 * 解码
	 * @param source
	 * @return
	 */
	public static String decrypt(String source) {
		if (source == null) {
			return "";
		}
		return (decodeString(parseHexString(source)));
	}
	/**
	 * 获得随机字符串
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length) {
		Random rand = new Random(System.currentTimeMillis());
		String str = "";
		int j;
		int k;
		for (int i = 0; i < length; i++) {
			j = rand.nextInt(26);
			k = rand.nextInt(2);
			switch (k) {
			case 0:
				j += 65; // A-Z
				break;
			case 1:
				j += 97; // a-z
			}
			char c = (char) j;
			str += c;
		}
		return str;
	}
	/**
	 * String数组转换成Long数字
	 * @param ids
	 * @return
	 */
	public static long[] strings2long(String[] ids) {
		if (null == ids) {
			return null;
		}
		long[] ret = new long[ids.length];
		for (int i = 0; i < ids.length; i++) {
			try {
				ret[i] = Long.parseLong(ids[i]);
			} catch (NumberFormatException ex) {
				ret[i] = 0;
			}
		}
		return ret;
	}
	/**
	 * String转换成Long数组
	 * @param str
	 * @return
	 */
	public static Long[] string2Longs(String str) {
		Vector<Long> ret = new Vector<Long>();
		int index = str.indexOf(",");
		while (index >= 0) {
			String sid = str.substring(0, index);
			try {
				Long lid = new Long(sid);
				ret.add(lid);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			str = str.substring(index + 1);
			index = str.indexOf(",");
		}
		if (str != null && str.length()>0) {
			try {
				Long lid = new Long(str);
				ret.add(lid);
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		}
		Long[] ls = new Long[ret.size()];
		for (int i = 0; i < ret.size(); i++) {
			ls[i] = ((Long) ret.elementAt(i)).longValue();
		}
		return ls;
	}
	/**
	 * String转换成Double数组
	 * @param str
	 * @return
	 */
	public static Double[] string2Doubles(String str) {
		Vector<Double> ret = new Vector<Double>();
		int index = str.indexOf(",");
		while (index >= 0) {
			String sid = str.substring(0, index);
			try {
				Double lid = new Double(sid);
				ret.add(lid);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			str = str.substring(index + 1);
			index = str.indexOf(",");
		}
		if (str != null && str.length()>0) {
			try {
				Double lid = new Double(str);
				ret.add(lid);
			} catch (Exception ex) {
				ex.printStackTrace();
			}

		}
		Double[] ls = new Double[ret.size()];
		for (int i = 0; i < ret.size(); i++) {
			ls[i] = ((Double) ret.elementAt(i)).doubleValue();
		}
		return ls;
	}
	/**
	 * 获取颜色对象
	 * @param color
	 * @return
	 * @author wanggenyou
	 */
	public static Color getColor(String color) {
		//{'black':'黑色','white':'白色','gray':'灰色','red':'红色','yellow':'黄色','blue':'蓝色','green':'绿色'}
		if ("black".equals(color)) {
			return Color.BLACK;
		}else if ("white".equals(color)) {
			return Color.WHITE;
		}else if ("red".equals(color)) {
			return Color.RED;
		}else if ("yellow".equals(color)) {
			return Color.YELLOW;
		}else if ("blue".equals(color)) {
			return Color.BLUE;
		}else if ("green".equals(color)) {
			return Color.GREEN;
		}else if ("gray".equals(color)) {
			return Color.GRAY;
		}else {
			return Color.BLACK;
		}
	}
	/**
	 * 格式化数字
	 * @param num
	 * @param len
	 * @return
	 */
	public static String num(String num,Integer len) {
		if (num.length()>=len) {
			return num;
		}else {
			Integer nl = num.length();
			for (int i = 0; i < len-nl; i++) {
				num = "0"+num;
			}
			return num;
		}
	}
	
	/**
	 * 格式化数据
	 * @param num
	 * @return
	 */
	public static String mumberFormatWithLong(Long num) {
		NumberFormat nf = NumberFormat.getInstance(Locale.CHINA);
		return nf.format(num);
	}
	
	/**
	 * 格式化时间
	 * @param mss
	 * @return
	 */
	public static String formatDuring(Long mss) {  
	    long days = mss / (1000 * 60 * 60 * 24);  
	    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);  
	    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);  
	    long seconds = (mss % (1000 * 60)) / 1000;
	    StringBuffer time = new StringBuffer();
	    if (days>0) {
	    	time.append(days+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒");
		}else {
			if (hours>0) {
				time.append(hours+"小时"+minutes+"分钟"+seconds+"秒");
			}else {
				if (minutes>0) {
					time.append(minutes+"分钟"+seconds+"秒");
				}else {
					time.append(seconds+"秒");
				}
			}
		}
	    return time.toString();  
	}
	
	public static void main(String[] args) {
		/*Long[] aaLongs = StringCode.string2Longs("1,2,");
		for (Long l : aaLongs) {
			System.out.println(l);
		}*/
		System.out.println(decrypt("84bf68f5080d"));
	}
}
